package com.sscaipiao.android.activity.Base;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.StartTogetherActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.RedBall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.view.View.OnClickListener;

/**
 * Created by ppona_000 on 13-12-30.
 */
public class RedBallGroupTenPayActivity extends NormalPayActivity implements NormalPayActivity.onClickCallback {
	protected List<Map<String, Object>> mList;

	private int mMaxBall;
	private int mRandomBall;
	private int nGroupNum;

	private String[] mFrom = new String[]{"tv_ball_ids","tv_ball2_ids","tv_single_or_mult","tv_lott_num"};

	private int[] mTo = new int[]{R.id.tv_ball_ids,R.id.tv_ball2_ids,R.id.tv_single_or_mult,R.id.tv_lott_num};


	public void onHistoryListClick(int index) {
		if (index < choosen.size()) {
			choosen.remove(index);
			setupOrderList();
			recalculatorTotal();
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupOrderList();
		setHistoryListClickCallback(this);
	}

	public void setupOrderList() {
		mList = new ArrayList<Map<String,Object>>();
		SimpleAdapter mAdapter = new SimpleAdapter(this,
				mList,
				R.layout.redball_double_array_pay_item,
				mFrom,mTo);
		setListAdapter(mAdapter);
		addOrderListValues();

	}
	public void addOrderListValues() {
		Map<String,Object> mMap = null;
		chooseNumber = 0;
		mList.clear();
		for(int j = 0;j < choosen.size() ;j++){
			mMap = new HashMap<String,Object>();
			Log.v("choosen", choosen.get(j));
			String[] split = choosen.get(j).split(Constants.STRING_SPLITE_COMPLEX);
			mMap.put("tv_ball_ids", split[0]);
			mMap.put("tv_single_or_mult",  split[1].subSequence(0, 2));
			mMap.put("tv_lott_num",  split[2]);
			chooseNumber = chooseNumber + Integer.parseInt(split[2].substring(0, split[2].indexOf("注")));
			mList.add(mMap);
		}
		recalculatorTotal();
	}


	public void setRandom(int max, int random) {
		mMaxBall = max;
		mRandomBall = random;
		nGroupNum = random;
	}
	@Override
	public void onResume() {
		super.onResume();
	}

	private void addOrderByAuto() {
		if (choosen == null) {
			choosen = new ArrayList<String>();
		}
		String str = randomChoose();
		while (choosen.contains(str)) {
			str = randomChoose();
		}
		choosen.add(str);
		user.setChoosen(choosen);
		recalculatorTotal();
	}
	public Integer[] randomChooseRedBallsInGroup() {
		Integer[] a = new Integer[nGroupNum];
		for (int i = 0; i < nGroupNum; i++) {
			int random = (int) (Math.random()*10);
			int ballId = i*10+random;
			a[i] = ballId;
		}
		return a;
	}
	private String randomChoose() {
		Integer[] randomalls1 = randomChooseRedBallsInGroup();
		String redBalls = "";
		String blueBalls = "";
		String singleOrMult = "";
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_1);
		String number = String.format(sAgeFormat1, "1");

		for (int num : randomalls1) {
			if (num < 10) {
				redBalls += ("0" + num + Constants.STRING_SPLITE);
			} else {
				redBalls += (num + Constants.STRING_SPLITE);
			}
		}
		singleOrMult = getResources().getString(R.string.double_color_ball_add_single);
		String str = redBalls.substring(0, redBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
				singleOrMult + Constants.STRING_SPLITE_COMPLEX + number;
		return str;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.add_by_auto == v.getId()) {
			addOrderByAuto();
			addOrderListValues();
		}
	}
}